<template>
  <ych-modal
    :fullscreen="false"
    :title="title"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :cancelText="$t('m.common.button.close')">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="12">
            <a-form-item :label="$t('m.eform.appform.entity.formName')" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <a-input v-decorator="[ 'formName', validatorRules.formName]" placeholder=""></a-input>-->
              <locale-tag v-decorator="[ 'formName', validatorRules.formName]" :group-id="groupId"></locale-tag>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item :label="$t('m.eform.appform.entity.formCode')" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'formCode', validatorRules.formCode]" placeholder=""></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item :label="$t('m.eform.appform.entity.formVersion')" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="[ 'formVersion', validatorRules.formVersion]" placeholder=""
                              style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item :label="$t('m.eform.appform.entity.objectName')" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <object-code-tag v-decorator="[ 'objectCode', validatorRules.objectCode]"
                               placeholder="" :groupId="groupId"></object-code-tag>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item :label="$t('m.common.label.sort')" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="[ 'orderNum', validatorRules.orderNum]" style="width: 100%;"></a-input-number>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </ych-modal>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import ObjectCodeTag from '../select/tag/ObjectCodeTag'
  import YchConstant from "@/api/YchConstant";
  import LocaleTag from "../localeProvider/tag/LocaleTag";

  export default {
    name: 'ActAppFormModal',
    components: {
      ObjectCodeTag,
      LocaleTag,
    },
    props: {
      groupId: {
        type: String,
        required: true
      },
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: this.$t('m.common.label.operate'),
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          formName: { rules: [{ required: true, message: this.$t('m.eform.appform.verify.formName')  }] },
          formCode: { rules: [{ required: true, message: this.$t('m.eform.appform.verify.formCode')  }, { validator: this.checkCode }] },
          formVersion: {
            initialValue: 1
          },
          objectCode: {},
          orderNum: {
            initialValue: 1 // TODO 默认值取数据库最大值+10
          },
        },
        url: {
          add: YchConstant.PREFIX_EFORM + '/manage/actAppForm/add',
          edit: YchConstant.PREFIX_EFORM + '/manage/actAppForm/edit',
          checkCode: YchConstant.PREFIX_EFORM + '/manage/actAppForm/checkCode',
        }
      }
    },
    methods: {
      add () {
        this.edit({})
      },
      edit (record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        // 多语言
        this.model.formName = this.model.formNameLocaleCode
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(this.lodash.pick(this.model, 'formName', 'formCode', 'formVersion', 'objectCode', 'orderNum'))
        })
      },
      close () {
        this.$emit('close')
        this.visible = false
      },
      handleOk () {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            formData.groupId = this.groupId
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.messageUtil.success(res.message)
                that.$emit('ok')
              } else {
                that.messageUtil.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      checkCode (rule, value, callback) {
        if (!value) {
          callback()
          return
        }
        let reg = new RegExp("[\\u4E00-\\u9FFF]+","g");
        if(reg.test(value)){
          callback(this.$t('m.eform.appform.verify.formCode_format'))
          return
        }
        getAction(this.url.checkCode, {
          value: value,
          id: this.model.id
        }).then((res) => {
          res.success ? callback() : callback(res.message)
        })
      }
    }
  }
</script>
